/*
package com.example.Es;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.util.*;

import static org.springframework.cglib.beans.BeanMap.create;

*/
/**
 * @author leiliang
 * @version 1.0
 * @Description
 * @date 2021/6/7 15:15
 *//*

@Service
@Slf4j
public class TitleService {

    @Autowired
    private TitleRepository titleRepository;
    @Autowired(required=false)
    private ElasticsearchTemplate template;

    */
/**
     * 创建索引
     *
     * @param
     * @param titleDocument
     *//*

    public void save(TitleDocument titleDocument) {
        titleRepository.save(titleDocument);
    }


    */
/**
     * 删除索引数据
     *
     * @param id
     * @return
     *//*

    public boolean delete(Long id) {
        titleRepository.deleteById(id);
        return true;
    }

    */
/**
     * 查询所有的文章
     *
     * @param searchContent
     * @return
     *//*

    public List<TitleDocument> getTitle(String searchContent) {
        List<TitleDocument> list1 = titleRepository.queryByTitlename(searchContent);
        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(searchContent);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
                .trackTotalHits(true);
        System.out.println("查询的语句:" + builder);
        Iterable<TitleDocument> searchResult = titleRepository.search(builder);
        Iterator<TitleDocument> iterator = searchResult.iterator();
        List<TitleDocument> list = new ArrayList<>();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }

    */
/**
     * 分页查询
     *
     * @param pageNumber
     * @param pageSize
     * @param searchContent
     * @return
     *//*

    public List<TitleDocument> pageTitle(Integer pageNumber, Integer pageSize, String searchContent) {
//        分页参数
        PageRequest pageRequest = PageRequest.of(pageNumber, pageSize);
        //QueryStringQueryBuilder queryBuilder = new QueryStringQueryBuilder(searchContent);
        QueryBuilder qb1 = QueryBuilders.wildcardQuery("titlename", "*标*");

        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withPageable(pageRequest).withQuery(qb1).build();
        System.out.println("查询的语句：" + searchQuery.getQuery().toString());
        Page<TitleDocument> search = titleRepository.search(searchQuery);
        return search.getContent();
    }

    public List<TitleDocument> getAll() {
        Iterable<TitleDocument> all = titleRepository.findAll();
        Iterator<TitleDocument> iterator = all.iterator();
        List<TitleDocument> list = new ArrayList<>();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }


    */
/**
     * 按某个字段分组求和（这里按照省份分组统计人数总和）
     * 实现sql:select field1, field2, sum(field3) from table_name group by field1, field2;
     *//*

     public Map<String, String> getTypeGroupBy() {
        NativeSearchQueryBuilder queryBuilderGroup = new NativeSearchQueryBuilder();
        // 聚合查询。name是要统计的字段，group_name是自定义的别名
        queryBuilderGroup.addAggregation(AggregationBuilders.terms("groupName").field("titlename.keyword"));
                //.subAggregation(AggregationBuilders.sum("sum_codes").field("id.keyword")));
         //查询操作,这里用的另外一种查询，不包括分页效果
         AggregatedPage<TitleDocument> result = (AggregatedPage<TitleDocument>) titleRepository.search(queryBuilderGroup.build());
         //AggregatedPage<TitleDocument> result = template.queryForPage(queryBuilderGroup.build(), TitleDocument.class);
        //解析聚合
         Terms terms =  result.getAggregations().get("groupName");
        //获取指定名称的聚合
          Collection<Terms.Bucket> buckets = (Collection<Terms.Bucket>) terms.getBuckets();

         Map<String, String> dataMap = new HashMap<String, String>();
         for (Terms.Bucket bucket : buckets) {
             String key = (String) bucket.getKey();
             dataMap.put(key, bucket.getDocCount() + "");
         }

         return dataMap;


       */
/*  //获取桶
        List<StringTerms.Bucket> buckets = terms.getBuckets();
        //遍历打印
        List list = new ArrayList();
        for (StringTerms.Bucket bucket : buckets) {
            Sum sum = bucket.getAggregations().get("sum_codes");
            Map map = new HashMap<>();
            log.info("省：" + bucket.getKeyAsString());
            log.info("记录行数：" + bucket.getDocCount());
            log.info("总人数统计：" + sum.getValue());
            map.put("省", bucket.getKeyAsString());
            map.put("记录行数", bucket.getDocCount());
            map.put("总人数统计", sum.getValue());
            list.add(map);
        }*//*


    }


    */
/**
     * 执行搜索（带分组）
     * @param indexName 索引名称
     * @param typeName 类型名称
     * @param queryBuilder 查询内容
     * @param aggsField 要分组的字段
     * @return
     *//*

   */
/* public Map<String, String> searcher(String indexName, String typeName,
                                        QueryBuilder queryBuilder, String aggsField) {

        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(searchContent);
        System.out.println("查询的语句:" + builder);
        Iterable<TitleDocument> searchResult = titleRepository.search(builder);

        titleRepository.search()
        SearchRequestBuilder searchRequestBuilder = client
                .prepareSearch(indexName).setTypes(typeName)
                .setQuery(queryBuilder);
        //创建TermsBuilder对象，使用term查询，设置该分组的名称为aggs-class，并根据aggsField字段进行分组
        TermsBuilder termsBuilder = AggregationBuilders.terms("aggs-class")
                .field(aggsField);//此处也可继续设置order、size等
        //添加分组信息
        searchRequestBuilder.addAggregation(termsBuilder);
        //执行搜索
        SearchResponse searchResponse = searchRequestBuilder.execute()
                .actionGet();
        //解析返回数据，获取分组名称为aggs-class的数据
        Terms terms = searchResponse.getAggregations().get("aggs-class");
        Collection<Terms.Bucket> buckets = terms.getBuckets();
        Map<String, String> dataMap = new HashMap<String, String>();
        for (Terms.Bucket bucket : buckets) {
            String key = bucket.getKey();
            dataMap.put(key, bucket.getDocCount() + "");
        }
        return dataMap;
    }
*//*




    */
/**
     * person查询
     * 继承baseService的list返回的query对象
     * @return
     *//*

  */
/*  public Map<String, String> personList() {
        //如果参数有值，放这个map中
        Map<String, Object> queryParams = new HashMap<>();
        //接收参数，如果没有则按没条件查询，下方有判空


        QueryBuilder queryBuilder = createQueryBuilder(create(queryParams));
        //左侧聚合 条件：人员类型，职称
        SearchQuery searchQuery = creatSearchQuery(queryBuilder,
                //指定要聚合的字段
                AggregationBuilders.terms("military_rank").field("military_rank.keyword"),//种类
                AggregationBuilders.terms("personType").field("personType.keyword"),//人员类型
                AggregationBuilders.terms("title").field("title.keyword"));//职称

        //查询操作,这里用的另外一种查询，不包括分页效果
        AggregatedPage<PersonInfoEs> page = (AggregatedPage<PersonInfoEs>) titleRepository.search(searchQuery);
        //创建map用来放聚合的数据
        Map<String,Object> title = new HashMap<>();
        Map<String,Object> personType = new HashMap<>();
        //从结果中根据名称来拿聚合出来的数据
        Terms termTitle = (Terms) page.getAggregations().getAsMap().get("title");
        Terms termPersonType = (Terms) page.getAggregations().getAsMap().get("personType");
        Terms termMilitary_rank = (Terms) page.getAggregations().getAsMap().get("military_rank");
        //遍历方式拿数据，放入map中，map为数据内容，value为数量
        for (Terms.Bucket bucket : termPersonType.getBuckets()) {
            personType.put(bucket.getKey().toString().isEmpty() ? "其他" : bucket.getKey().toString(), bucket.getDocCount());
        }
        for (Terms.Bucket bucket : termTitle.getBuckets()) {
            title.put(bucket.getKey().toString().isEmpty() ? "其他" : bucket.getKey().toString(), bucket.getDocCount());
        }
        for (Terms.Bucket bucket : termMilitary_rank.getBuckets()) {
            military_rank.put(bucket.getKey().toString().isEmpty() || null== bucket.getKey() ? "其他" : bucket.getKey().toString(), bucket.getDocCount());
        }

        //最后在放入map中一并返回
        Map<String, Object> totalMap = new LinkedHashMap<>();
        totalMap.put("title", title);
        totalMap.put("personType",personType );
        totalMap.put("military_rank", military_rank);

        result.setCode(200);
        Map<String, Object> contentMap = new HashMap<>();
        contentMap.put("content", page.getContent());
        result.setData(contentMap);
        result.setCount(String.valueOf(page.getTotalElements()));
        result.setPageSize(page.getSize());
        result.setPageNumber(page.getNumber());
        HashMap<String, List<GroupInfo>> groupFilterMap = builAggregationVo(totalMap);

        result.setGroupFilter(groupFilterMap);
        return result;
    }
*//*


}
*/
